/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package XML_file;
import java.io.File;
import java.util.Properties;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XML{
    Properties prop = new Properties();
    Scanner input=new Scanner(System.in);
	private DocumentBuilder dBuilder;
    public void WriteXML() throws IOException, ParserConfigurationException, SAXException{
    // ghi vao xml
    System.out.println("Nhap vao ten file dau ra: ");
    String filename=input.nextLine();
    System.out.println("So phan tu ban muon nhap: ");
    int n=input.nextInt();
    File xmlFile = new File(filename);
    dBuilder = null;
    Document doc = dBuilder.parse(xmlFile);
    System.out.println("Root element : ");
    String noderoot=input.nextLine();
    doc.setDocumentURI(noderoot);
    for(int i=0;i<n;i++){
        System.out.print("Nhap vao phan tu thu "+i+": ");
        String a=input.nextLine();
        //String b=input.nextLine();
        prop.setProperty(null,a);
        System.out.println("\n");
    }
    System.out.println("End Root element : ");
    noderoot=input.nextLine();
    doc.setNodeValue(noderoot);
    System.out.println("Da Nhap Xong phan tu!");
    prop.storeToXML(new FileOutputStream(filename),"last updated at "+new Date().toString());
    System.out.println("Ban da tao file luc: "+new Date().toString());
    }
    public void ReadXML() throws IOException{
    // doc tu xml
        try {
            System.out.println("Nhap vao file can doc: ");
            String fileName=input.nextLine();
            File xmlFile = new File(fileName);
            DocumentBuilder dBuilder;
            dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            System.out.println("Root element : " + doc.getDocumentElement().getNodeName());
            //Doc va in ra gia tri cua cac node
            if (doc.hasChildNodes()) {
                printNote(doc.getChildNodes());
            }
            System.out.println("End Root element: " + doc.getDocumentElement().getNodeName());
        } catch (ParserConfigurationException | SAXException | IOException e) {
        }
    }
    public static void printNote(NodeList nodeList) {
        for (int count = 0; count < nodeList.getLength(); count++) {
            Node tempNode = nodeList.item(count);
            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
                /*
                 * Ten cua Node mo tab tempNode.getNodeName() + " [OPEN]");
                 * Gia tri cua node mo tab "Node Value = " + tempNode.getTextContent());
                */
                tempNode.getNodeName();
                System.out.print(tempNode.getTextContent());
                if (tempNode.hasAttributes()) {
                    NamedNodeMap nodeMap = tempNode.getAttributes();
                    for (int i = 0; i < nodeMap.getLength(); i++) {
                        /**
                         * In ra ten cua attributes: node.getNodeName()
                         * In ra gia tri cua attributes: node.getNodeValue()
                         */
                        Node node = nodeMap.item(i);
                        node.getNodeName();
                        System.out.print(node.getNodeValue());
                    }
                }
                    /**
                     * lap lai lan nua neu nhu tim thay node con
                     * if (tempNode.hasChildNodes()) {
                     * De quy node con printNote(tempNode.getChildNodes());
                     * In ra ten cua nut tab dong: tempNode.getNodeName()
                     * }
                     */
                tempNode.getNodeName();//Lay ten cua node dong tab
            }
        }
    }
    public static void main(String args[]) throws Exception{
        XML xml=new XML();
        xml.WriteXML();
        xml.ReadXML();
    }
}
